package com.atguigu.gmall.item.controller;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

//商品详情 数据汇总及处理
@RestController
@RequestMapping("/api/item")
public class ItemApiController {
    @Autowired
    private ProductFeignClient productFeignClient;
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;
    @Autowired
    private ListFeignClient listFeignClient;

    //数据汇总 处理
    @GetMapping("/getItem/{skuId}")
    public Map getItem(@PathVariable Long skuId){
        Map result = new HashMap();

        CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            //1库存信息
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            result.put("skuInfo", skuInfo);
            return skuInfo;
        }, threadPoolExecutor);
        CompletableFuture<Void> categoryViewCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            //2.一二三级分类
            BaseCategoryView baseCategoryView = productFeignClient.getBaseCategoryView(skuInfo.getCategory3Id());
            result.put("categoryView", baseCategoryView);
        }, threadPoolExecutor);//接受上一个任务的返回值 但自己不返回
        CompletableFuture<Void> priceCompletableFuture = CompletableFuture.runAsync(() -> {
            //3实时价格
            BigDecimal price = productFeignClient.getPrice(skuId);
            result.put("price", price);
        }, threadPoolExecutor);
        CompletableFuture<Void> spuSaleAttrListCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            //4销售属性及值及其选中
            List<SpuSaleAttr> spuSaleAttrList = productFeignClient.selectSpuSaleAttrListCheckBySku(skuInfo.getSpuId(), skuInfo.getId());
            result.put("spuSaleAttrList", spuSaleAttrList);
        }, threadPoolExecutor);
        CompletableFuture<Void> valuesSkuJsonCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            //5.组合数据 对应SKUID 页面要的是json串
            Map saleAttrValuesBySpu = productFeignClient.getSaleAttrValuesBySpu(skuInfo.getSpuId());
            result.put("valuesSkuJson", JSONObject.toJSONString(saleAttrValuesBySpu));
        }, threadPoolExecutor);//接收上一个任务的返回值 但是自己无返回
        //添加商品的热度评分
        CompletableFuture.runAsync(()->{
            listFeignClient.hotScore(skuId,1);
        }, threadPoolExecutor);


        //多线程组合
        CompletableFuture.allOf(skuInfoCompletableFuture,
                categoryViewCompletableFuture,priceCompletableFuture,
                spuSaleAttrListCompletableFuture,valuesSkuJsonCompletableFuture).join();

        //返回汇总结果
        return result;
    }
}
